package com.example.java.kafka.es7client;

import org.elasticsearch.action.get.*;
import org.elasticsearch.client.RequestOptions;

public class BatchGetDoc {
    public static void main(String[] args) {
        ConnectElasticsearch.connect(client -> {
            // 创建 MultiGetRequest 对象
            MultiGetRequest request = new MultiGetRequest();
            // 添加多个要查询的文档（index, id）
            request.add("user", "1001");
            request.add("user", "1002");
            request.add("user", "1003");
            try {
                // 发送请求，获取响应
                MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
                // 遍历响应结果
                for (MultiGetItemResponse itemResponse : response) {
                    GetResponse getResponse = itemResponse.getResponse();
                    if (getResponse.isExists()) {
                        String sourceAsString = getResponse.getSourceAsString();
                        System.out.println("Document [" + getResponse.getId() + "]: " + sourceAsString);
                    } else {
                        System.out.println("Document [" + getResponse.getId() + "] does not exist.");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
